/*
-----------------------------------------------------------------------------
This source file is part of Labor3D
(Labor 3D Graphics Engine)

Copyright (c) 2009-2020 Rock Mao
* creator : Rock Mao
* created : 2009-9-20   22:29
* filename: LaborCommonFunction.cpp
-----------------------------------------------------------------------------
*/
#include "stdafx.h"
#include "LaborCommonFunction.h"

#ifdef _WIN32
int g_DFCP = CP_ACP;
#else
int g_DFCP = 0;

#endif

LABORCORE_FUNC_API const wchar_t* getFileExt(const wchar_t* fileName)
{
	wstring ext=L"";
	int i = 0;
	for(i = (int)wcslen(fileName) - 1 ; i >=0  ; i--)
	{
		if(fileName[i] == '.') break;
		if(fileName[i] == '\\' || fileName[i] == '/')
			return L"";

		wchar_t buf[2];
		buf[0] = fileName[i] ;
		buf[1] = 0;
		ext = wstring(buf) + ext;
	}
	if(i == -1)
		ext = L"";
	for(i =  0 ; i < (int)ext.length() ; i ++)
	{
		if(ext[i] <='Z' && ext[i] >= 'A')
		{
			ext[i] = ext[i] + 32;
		}
	}
	return ext.c_str();
}

const char*  getFileExt(const char* fileName)
{
	string ext="";
	int i = 0;
	for(i = (int)strlen(fileName) - 1 ; i >=0  ; i--)
	{
		if(fileName[i] == '.') break;
		if(fileName[i] == '\\' || fileName[i] == '/')
			return "";

		char buf[2];
		buf[0] = fileName[i] ;
		buf[1] = 0;
		ext = string(buf) + ext;
	}
	if(i == -1)
		ext = "";
	for(i =  0 ; i < (int)ext.length() ; i ++)
	{
		if(ext[i] <='Z' && ext[i] >= 'A')
		{
			ext[i] = ext[i] + 32;
		}
	}
	return ext.c_str();
}

LABORCORE_FUNC_API bool Labor_LocaleToUnicode(const char* _in, wchar_t* _out,int outlen)
{
#ifdef _WIN32
	return 0 != ::MultiByteToWideChar(g_DFCP,0,_in,strlen(_in),_out,outlen);
	//return g_strCovert.convert(_in,(char*)_out,outlen);
#endif
#ifdef _LINUX
	return 0 != mbstowcs(_out, _in, strlen(_in));
#endif
}

LABORCORE_FUNC_API bool Labor_UnicodeToLocale(const wchar_t* _in, char* _out,int outlen)
{
#ifdef _WIN32
	return 0 != ::WideCharToMultiByte(g_DFCP,0,_in,wcslen(_in) , _out,outlen,NULL,NULL);
#endif
#ifdef _LINUX
	return 0 != wcstombs (_out, _in, wcslen(_in));
#endif
}
